package com.aerozhonghuan.logisticsmall.modules.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aerozhonghuan.foundation.log.LogUtil;
import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.base.TitlebarFragment;
import com.aerozhonghuan.logisticsmall.modules.order.adapter.MyOrdersAdapter;
import com.aerozhonghuan.logisticsmall.modules.order.beans.OrdersListBean;
import com.aerozhonghuan.logisticsmall.modules.order.logic.OrderWebRequest;
import com.aerozhonghuan.logisticsmall.widget.ProgressDialogIndicator;
import com.aerozhonghuan.oknet2.CommonCallback;
import com.aerozhonghuan.oknet2.CommonMessage;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * 描述 订单列表页面
 * 作者 Zhang Yonghui
 * 创建日期 2019-05-17
 */
public class MyOrdersFragment extends TitlebarFragment {

    private static final String TAG = "MessageFragment";
    private static final int TYPE_LOADDATA_FIRST = 0;
    private static final int TYPE_LOADDATA_REFRESH = 1;
    private static final int TYPE_LOADDATA_LOADMORE = 2;
    public static final String KEY_STATUS = "KEY_STATUS";
    private View rootView;
    private ProgressDialogIndicator progressDialogIndicator;
    private XRecyclerView recylerOrders;
    private MyOrdersAdapter myOrdersAdapter;
    private boolean isFirstLoadData = true;
    private int currentPage = 1;
    private int totalPage;
    private int status;
    private boolean isInit = false;
    private boolean isLoad = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_orders, null);
            LogUtil.d(TAG, "onCreateView");
            initView();
            isInit = true;
        }
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        isCanLoadData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        LogUtil.d(TAG, "setUserVisibleHint");
        isCanLoadData();
    }

    private void isCanLoadData() {
        LogUtil.d(TAG, "isCanLoadData");
        if (!isInit) {
            return;
        }
        if (getUserVisibleHint()) {
            initData(TYPE_LOADDATA_FIRST, 1);
            isLoad = true;
        } else {
            if (isLoad) {
//                stopLoad();
            }
        }
    }

    private void initView() {
        progressDialogIndicator = new ProgressDialogIndicator(getContext());
        recylerOrders = (XRecyclerView) rootView.findViewById(R.id.recylerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recylerOrders.setLayoutManager(layoutManager);
        myOrdersAdapter = new MyOrdersAdapter(getContext());
        myOrdersAdapter.setOnItemClickListener(new MyOrdersAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, OrdersListBean.OrderListBean orderListBean) {
                startActivity(new Intent(getContext(), OrderDetailAcrivity.class).putExtra("billId", String.valueOf(orderListBean.getBillId())));
            }
        });
        recylerOrders.setFootViewText("正在加载更多...", "暂无更多数据");
        recylerOrders.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        recylerOrders.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
        recylerOrders.setAdapter(myOrdersAdapter);
        recylerOrders.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initData(TYPE_LOADDATA_REFRESH, 1);
            }

            @Override
            public void onLoadMore() {
                if (currentPage >= totalPage) {
                    recylerOrders.setNoMore(true);
                } else {
                    initData(TYPE_LOADDATA_LOADMORE, currentPage + 1);
                }
            }
        });
        LogUtil.d(TAG, "recyler State1::" + recylerOrders.getDefaultRefreshHeaderView().getState());
    }

    private void initData(final int type, int loadPage) {
        if (getArguments() != null) {
            status = getArguments().getInt(KEY_STATUS);
        }
        if (isFirstLoadData) {
            progressDialogIndicator.onProgressStart();
        }
        OrderWebRequest.queryMyOrderList(getContext(), loadPage, status, new CommonCallback<OrdersListBean>() {
            @Override
            public void onSuccess(OrdersListBean ordersListBean, CommonMessage commonMessage, String allResponseString) {
                if (isFirstLoadData) {
                    progressDialogIndicator.onProgressEnd();
                    isFirstLoadData = false;
                }
                if (ordersListBean != null) {
                    totalPage = ordersListBean.getTotalPage();
                    List<OrdersListBean.OrderListBean> datalist = ordersListBean.getDatalist();
                    switch (type) {
                        case TYPE_LOADDATA_FIRST:
                            myOrdersAdapter.refreshList(datalist);
                            break;
                        case TYPE_LOADDATA_REFRESH:
                            myOrdersAdapter.refreshList(datalist);
                            recylerOrders.refreshComplete();
                            currentPage = 1;
                            break;
                        case TYPE_LOADDATA_LOADMORE:
                            myOrdersAdapter.addList(datalist);
                            recylerOrders.loadMoreComplete();
                            currentPage ++;
                            break;
                        default:
                            break;
                    }
                }
            }

            @Override
            public boolean onFailure(int httpCode, Exception exception, CommonMessage responseMessage, String allResponseString) {
                if (isFirstLoadData) {
                    progressDialogIndicator.onProgressEnd();
                    isFirstLoadData = false;
                }
                return super.onFailure(httpCode, exception, responseMessage, allResponseString);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isInit = false;
        isLoad = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (recylerOrders != null) {
            recylerOrders.destroy();
            recylerOrders = null;
        }
    }
}
